package com.task.quartz;

import java.io.IOException;

import org.quartz.Job;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.pingpang.common.bean.ResponseBean;
import com.task.bean.TaskLogBean;
import com.task.bean.TaskRunBean;
import com.task.config.SpringUtil;
import com.task.netty.util.ClientRequest;
import com.task.services.TaskLogService;

/**
 *  基础Job抽象类
 */
public abstract class AbsQuartzJob implements Job{
	private Logger log=LoggerFactory.getLogger(JobUtil.class);
	
	
    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
		ClientRequest clientRequest=(ClientRequest) SpringUtil.getBean("clientRequest");
		JobDataMap jm=context.getMergedJobDataMap();
		TaskRunBean tb=(TaskRunBean) jm.get("taskBean");
		ResponseBean response=null;
    	// Job运行前监控
        try {
			response=clientRequest.SendMsg(tb);
			// 调用业务代码
			this.businessWorking(context);
		} catch (InterruptedException | IOException e) {
			log.error("---执行失败:"+e.getMessage(), e);
			e.printStackTrace();
			response=new ResponseBean();
			response.setCode(-1);
			response.setErrorMsg(e.getMessage());
		}
        //记录日志数据
        TaskLogService tls=(TaskLogService) SpringUtil.getBean("taskLogService");
        tls.saveTaskLog(tb, response);
    }

    /** 业务代码抽象方法  */
    public abstract void businessWorking(JobExecutionContext context);
}
